#!/bin/sh

export PHP_FPM_PIDFILE="/tmp/pids/php-fpm.pid"
export PHP_FPM_SOCKET="/tmp/sockets/php-fpm.sock"

mkdir -p -m 750 "$(dirname $PHP_FPM_PIDFILE)"
mkdir -p -m 750 "$(dirname $PHP_FPM_SOCKET)"

php_is_running()
{
	[ -f "$PHP_FPM_PIDFILE" -a -S "$PHP_FPM_SOCKET" ]
}

wait_for_php()
{
	if ! php_is_running; then
		echo -n "Waiting for PHP... "
		while ! php_is_running; do
			sleep 1
		done
		echo "done"
	fi
}

php_pid()
{
	if php_is_running; then
		cat "$PHP_FPM_PIDFILE"
	else
		echo "Unable to get PHP PID as it's not yet running" >&2
		echo ""
	fi
}
